﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

using VoizeRemoteCore;

namespace VoizeRemote
{
    class VoizeRemoteMain
    {
        private static bool m_Running = true;
        private static Connection m_Con;
      
        static void Main(string[] args)
        {
            Console.Title = "Voize remote terminal";
          
            if (args.Length >= 4)
            {
                try
                {
                    m_Con = Connect(args[0], args[1], args[2], args[3]);
                    Console.WriteLine("\t\tConnected: " + args[0] + ":" + args[1] + " \n");
                    Menu();
                    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            else
            {
                DisplayHelp();
            }
        }


        private static Connection Connect(string ip,string port,string user,string password)
        {
            m_Con = new Connection(ip, port,user,password);
            return m_Con;
        }

        private static void DisplayHelp()
        {
            Console.WriteLine("Usage:");
            Console.WriteLine("VoizeRemote.exe [ip] [port]");
        }

        private static void Menu()
        {
            Console.WriteLine("\n\t\t=============================================");
            Console.WriteLine("\t\t         Voize remote terminal.");
            Console.WriteLine("\t\t=============================================");
            Console.WriteLine("\t\tpl: Play");
            Console.WriteLine("\t\tpa: Pause");
            Console.WriteLine("\t\tst: Stop");
            Console.WriteLine("\t\tne: Next song");
            Console.WriteLine("\t\tpr: Previous song");
            Console.WriteLine("\t\tli: List all available songs");
            Console.WriteLine("\t\the: Available commands");
            Console.WriteLine("\t\tqu: Quit and disconnect\n");

            Console.Write("\t\tSelect action: ");

            while (m_Running)
            {
              

                string action;
                action = Console.ReadLine();

                switch (action)
                {
                    case "pl":
                        Console.Write("\t\tTrack number: ");
                        string track = Console.ReadLine();
                        Console.WriteLine(m_Con.SendCommand("play" + ";" + track));
                        Menu();
                        break;
                    case "pa":
                        Console.WriteLine(m_Con.SendCommand("pause"));
                        Menu();
                        break;
                    case "st":
                        Console.WriteLine(m_Con.SendCommand("stop"));
                        Menu();
                        break;
                    case "ne":
                        Console.WriteLine(m_Con.SendCommand("next"));
                        Menu();
                        break;
                    case "pr":
                        Console.WriteLine(m_Con.SendCommand("prev"));
                        Menu();
                        break;
                    case "li":
                        Console.WriteLine(m_Con.SendCommand("list"));
                        Menu();
                        break;
                    case "he":
                        Console.WriteLine(m_Con.SendCommand("help"));
                        Menu();
                        break;
                    case "qu":
                        m_Running = false;
                        break;
                    default:
                        Console.WriteLine("\t\t" + action + ": " + "Command not supported.");
                        Console.WriteLine("\n");
                        Console.Write("\t\tSelect action:");
                        break;
                }
              }
                
            }
        }
}

